package base;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/14
 * @VERSION 1.0
 * @DESC
 * Callable:实现
 * 1.FutureTask
 * 2.CompleteFuture:异步调用[https://blog.csdn.net/finalheart/article/details/87615546]
 * future需要等待isDone为true才能知道任务跑完了。
 * 或者就是用get方法调用的时候会出现阻塞。
 * 而使用completableFuture的使用就可以用then，when等
 * 等操作来防止以上的阻塞和轮询isDone的现象出现。
 *
 *
 */
class Task implements Callable{
    @Override
    public Object call() throws Exception {
        return 1;
    }
}
public class FutureDemo {
    public static void main(String[] args) {
        // Future impletements Future和Callable
        //Future<Integer> future0 = new FutureTask<Integer>(new CallableTask());
        //FutureTask<Integer> future1 = new FutureTask<Integer>(new RunnableTask());

        //CompletableFuture<Integer> future1 = new CompletableFuture<>(new Task());
        //'CompletableFuture(java.lang.Object)' is not public in 'java.util.concurrent.CompletableFuture'.
        // Cannot be accessed from outside package
        // impletements Future<T>, CompletionStage
        //future.
        //JDK1.8新特性CompletableFuture总结
        //CompletableFuture completableFuture = new CompletableFuture();

        CompletableFuture completableFuture  = CompletableFuture.supplyAsync(()->{
            int val = 1/0;
            return 1;
        });
        completableFuture.whenComplete((v,e)->{  //返回值和异常
            System.out.println(v+":"+e);
        });

    }
}
